package com.scalpel.util.word;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by scalpel on 2017/2/9.
 */
public class WordUtil {

    public static class ConfigurationFactory {
        static Configuration cfg = null;
        private ConfigurationFactory(){};
        public static synchronized Configuration getInstantiation(){
            if ( cfg == null ){
                cfg = new Configuration();
            }
            return   cfg;
        }
    }

    private WordUtil(){}
    private static Configuration cfg = null;

    public static Configuration getFreemakerCfgInstantiation() {
        if(cfg == null)
        {
            String name = WordUtil.class.getName();
            int num = name.split("\\.").length;
            String root = "";
            for (int i=1;i<num;i++)
            {
                root +="../";
            }
            cfg = ConfigurationFactory.getInstantiation();
            // 指定FreeMarker模板文件的位置
            cfg.setClassForTemplateLoading(WordUtil.class,root);
        }
        return cfg;

    }

    /**
     * 根据模板创建word文档输出
     * @param data
     * @param templ
     */
    public static void createWordOutputByTemplate(HashMap<String,Object> data, String templ, Writer writer) throws IOException, TemplateException {
        parseImage(data);
        Configuration cfg =  getFreemakerCfgInstantiation();
        Template template = cfg.getTemplate(templ,"UTF-8");
        template.process(data,writer);

    }


    /**
     * 解析IMAGE标签
     * @param data
     */
    private static  void parseImage(HashMap<String,Object> data)
    {
        Iterator<String> keys = data.keySet().iterator();
        while (keys.hasNext())
        {
            String key = keys.next();
            Object val = data.get(key);
            if(val instanceof File)
            {
                try {
                    ImageUtil util = new ImageUtil((File) val);
                    data.put(key,util.getImageXml());
                    data.put(key+"_name",util.getImageXmlNoCreate());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if(val  instanceof URL)
            {
                try {
                    ImageUtil util = new ImageUtil((URL) val);
                    data.put(key,util.getImageXml());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public static void main(String args[])
    {
        String name = WordUtil.class.getName();
        int num = name.split("\\.").length;
        System.out.println(WordUtil.class.getName());
        System.out.println(num);
    }
}
